import {NextFunction, Request, Response} from "express";
import SportCategoryService from "../services/SportCategoryService";
import ResponseDTO from "@/dto/ResponseDTO";

class SportCategoryController {
    // 创建运动类别
    public async createSportCategory(req: Request, res: Response, next: NextFunction) {
        try {
            const sportCategoryParams = req.body;
            await SportCategoryService.createSportCategory(sportCategoryParams);
            res.locals.data = true;
            next();
        } catch (error) {
            res.locals.error = error;
            next();
        }
    }

    /** 获取运动类别列表 */
    public async getSportCategoryList(req: Request, res: Response, next: NextFunction) {
        try {
            const listParams = {
                pageSize: Number(req.query?.pageSize || 10),
                pageIndex: Number(req.query?.pageIndex || 1),
                sportCategoryName: String(req.query?.sportCategoryName || ""),
            };
            const sportCategoryList = await SportCategoryService.getSportCategoryList(listParams);
            res.locals.data = sportCategoryList;
            next();
        } catch (error) {
            res.locals.error = error;
            next();
        }
    }

    /** 更新运动类别信息 */
    public async updateSportCategory(req: Request, res: Response, next: NextFunction) {
        try {
            const sportCategoryParams = req.body;
            if (!sportCategoryParams?.sportCategoryId) {
                res.send(ResponseDTO.error(100, "参数错误，sportCategoryId不能为空"));
                return;
            }
            await SportCategoryService.updateSportCategory(sportCategoryParams);
            res.locals.data = true;
            next();
        } catch (error) {
            res.locals.error = error;
            next();
        }
    }

    /** 删除运动类别信息 */
    public async deleteSportCategory(req: Request, res: Response, next: NextFunction) {
        try {
            const sportCategoryParams = req.body;
            if (!sportCategoryParams?.sportCategoryId) {
                res.send(ResponseDTO.error(100, "参数错误，sportCategoryId不能为空"));
            }
            await SportCategoryService.deleteSportCategory(sportCategoryParams.sportCategoryId as string);
            res.locals.data = true;
            next();
        } catch (error) {
            res.locals.error = error;
            next();
        }
    }

    // 获取运动类别信息
    public async getSportCategoryById(req: Request, res: Response, next: NextFunction) {
        try {
            const sportCategoryId = (req.query?.sportCategoryId as string) || "";
            const sportCategory = await SportCategoryService.getSportCategoryById(sportCategoryId);
            res.locals.data = sportCategory;
            next();
        } catch (error) {
            res.locals.error = error;
            next();
        }
    }
}

export default new SportCategoryController();
